<?php


namespace app\admin\controller;


use think\admin\Controller;

/**
 * 考勤设置管理
 * Class CheckWorkAttendance
 * @package app\admin\controller
 */
class CheckWorkAttendance extends Controller
{

    private $table = 'lg_check_work_attendance' ;

    /**
     * 考勤设置列表
     * @auth true
     * @menu true
     * @login true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
        $this->title = '考勤设置列表';
        $this->type = input('year', 'index');
        $this->type = input('name', 'index');
        $this->type = input('status', 'index');
        $query = $this->_query($this->table);
        $query->like('name')->equal('status')->equal('year')->order('id desc')->page();
    }

    /**
     * 新增考勤设置
     * @auth true
     * @login true
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $data = [
//                'store_id' => input('store_id'),
                'name' => input('name'),
                'year' => input('year'),
//                'begin_time' => input('begin_time'),
//                'over_time' => input('over_time'),
                'go_work_time' => input('go_work_time'),
                'off_work_time' => input('off_work_time'),
                'check_week_begin' => input('check_week_begin'),
                'check_week_over' => input('check_week_over'),
//                'is_exclude' => input('is_exclude'),
                'status' => input('status'),
                'remark' => input('remark'),
                'update_time' => time(),
                'create_time' => time()
            ];
//            $check_name = $this->app->db->name($this->table)->where(['store_id' => input('store_id')])->find();
//            if ($check_name) {
//                $this->error('该店铺已添加考勤');
//            }
//            $check_cmd = $this->app->db->name($this->table)->where(['cmd' => input('cmd')])->find();
//            if ($check_cmd) {
//                $this->error('新增失败，重复命令');
//            }
            $result = $this->app->db->name($this->table)->insert($data);
            if (!$result) {
                $this->error('操作失败');
            }
            sysoplog('新增店铺考勤设置', '新增id为' . $result);
            $this->success('新增成功');
        }
        $this->_form($this->table, 'add');
    }

    /**
     * 修改考勤设置
     * @auth true
     * @login true
     */
    public function edit()
    {
        if ($this->request->isPost()) {
            $data = [
//                'store_id' => input('store_id'),
//                'store_name' => input('store_name'),
                'name' => input('name'),
                'year' => input('year'),
//                'begin_time' => input('begin_time'),
//                'over_time' => input('over_time'),
                'go_work_time' => input('go_work_time'),
                'off_work_time' => input('off_work_time'),
                'check_week_begin' => input('check_week_begin'),
                'check_week_over' => input('check_week_over'),
//                'is_exclude' => input('is_exclude'),
                'status' => input('status'),
                'remark' => input('remark'),
                'update_time' => time()
            ];
            $result = $this->app->db->name($this->table)->where(['id' => input('id')])->update($data);
            if (!$result) {
                $this->error('操作失败');
            }
            sysoplog('修改店铺考勤设置', '编辑id为' . $result);
            $this->success('编辑成功');
        }
        $this->_form($this->table, 'edit');
    }

    /**
     * 删除
     * @throws \think\db\exception\DbException
     * @auth true
     * @login true
     */
    public function remove()
    {
        sysoplog('删除考勤设置', '删除id为' . input('id'));
        $this->_delete($this->table);
    }

    //搜索店铺
    public function search_store()
    {
        $where[] = ['name','like','%'.input('store_name').'%'] ;
        $store = $this->app->db->name('lg_distributor')->where($where)->select();
        return json_success('ok',$store);
    }

}